package Dao;

import Util.JBDCUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @since 2024-06-03
 * @author yooo_fan
 * 数据库访问对象
 */
public class BookDao {
    private static PreparedStatement findBookPreparedStatement = null;

    public static Boolean ifFindBook(String name,String id){
        try(ResultSet rs= BookDao.findBook(name,id);){
            return rs != null && rs.next();
        } catch (SQLException e) {
            System.err.println("查询数据时出现错误");
            return false;
        }
    }

    /**
     * 查询书籍信息
     * @param name 书籍名称
     * @param id 书籍id
     * @return ResultSet
     * @throws SQLException 数据库异常
     */
    public static ResultSet findBook(String name, String id) throws SQLException{

        if(findBookPreparedStatement == null){
            String sql = "SELECT * FROM Book WHERE BName=? AND BID=?";
            findBookPreparedStatement = JBDCUtil.getPreparedStatement(sql);
        }

        findBookPreparedStatement.setString(1, name);
        findBookPreparedStatement.setString(2, id);
        System.out.println("查询名称 "+name);
        System.out.println("查询id "+id);

        ResultSet result = findBookPreparedStatement.executeQuery();
        JBDCUtil.commit();
        return result;
    }
}